<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-12-14
 * Time: 14:22
 */

namespace app\index\controller;


use app\common\controller\HomeBase;
use app\common\model\Area;
use app\common\model\Order;
use app\common\model\Systems;
use app\common\model\User;
use think\Cache;
use think\Db;

class PersonalCenter extends HomeBase
{

    /**
     * @param
     * 省市区三级联动
     */
    public function pervincelist(){
        $province = Area::getProvince();
        return json(['code' => 200, 'msg' =>$province ]);
    }

    public function citylist()
    {
        $pid = input('get.')['pid'];
        if (!$pid || $pid == '') {
            return json(['code' => 300, 'msg' => '暂无数据']);
        }
        $city  = Area::getCity($pid);
        if (!$city) {
            return json(['code' => 300, 'msg' =>"暂无数据" ]);
        }
        return json(['code' => 200, 'msg' =>$city ]);
    }


    /**
     * 根据用户ID  获取该用户的基本信息
     */
    public function getuserinfobyuid()
    {
        $params = input();
        if(array_key_exists("u_id",$params) && $params['u_id'] != ''){
            $data = User::getUserData($params['u_id']);
            return json(['code' => 200, 'msg' => $data]);
        } else {
            return json(['code' => 300, 'msg' => '未登录']);
        }
    }

    /**
     *转账之前查看 要转的账户是否存在
     */
    public function emailIsExistence()
    {
        $data = input();
        $validate_result = $this->validate($data, 'Existence');
        if ($validate_result !== true) {
            return json(['code' => 300, 'msg' => $validate_result]);
        };
        $res = User::emailUser($data['u_id'], $data['u_email']);
        if ($res === -1) {
            return json(['code' => 300, 'msg' => '不能给自己转账']);
        }
        if ($res === -2) {
            return json(['code' => 300, 'msg' => '该用户不存在']);
        }
        return json(['code' => 200, 'msg' => $res]);
    }

    /**
     * 转账类型是 转证券的话  获取该用户拥有证券的列表
     */
    public function getHaveProjectList()
    {
        $params = input();
        if(array_key_exists("u_id",$params) && $params['u_id'] != ''){
            $list = User::getUserHaveProjectList($params['u_id']);
            if (!$list) {
                return json(['code' => 300, 'msg' => '暂无数据']);
            }
            return json(['code' => 200, 'msg' => $list]);
        } else {
            return json(['code' => 300, 'msg' => '未登录']);
        }
    }

    /**
     * 用户转证券
     */
    public function userTurnOutProject()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Turnout');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            $res  = User::TurnOutProject($params);
            $money = floatval($params['money'])/100;

            if(floor($money) != $money || $money < 0){
                return  json(['code' => 300, 'msg' => '转出证券的市值必须为100的倍数']);
            }
            if ($res === -1) {
                return  json(['code' => 300, 'msg' => '未知错误，请联系平台人员']);
            }
            if ($res === -2) {
                return  json(['code' => 300, 'msg' => '转出的市值不能大于当前拥有证券的市值']);
            }
            if ($res === -3) {
                return  json(['code' => 300, 'msg' => '密码输入错误']);
            }
            if ($res === -4) {
                return  json(['code' => 300, 'msg' => '未知错误，请联系平台人员']);
            }
            if (!$res) {
                return  json(['code' => 300, 'msg' => '转出证券失败，请稍后重试']);
            }
            return json(['code' => 200, 'msg' => '转出成功']);
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }

    /**
     * 用户转美金
     */
    public function userTurnOutDollar()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'TurnoutMoney');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            $res  = User::turnOutDollar($params);
            $money = floatval($params['money'])/100;
            if(floor($money) != $money || $money < 0){
                return  json(['code' => 300, 'msg' => '转出额度必须为100的倍数']);
            }
            if ($res === -1) {
                return  json(['code' => 300, 'msg' => '转入账户不存在']);
            }
            if ($res === -2) {
                return  json(['code' => 300, 'msg' => '余额不足，转出失败']);
            }
            if ($res === -3) {
                return  json(['code' => 300, 'msg' => '密码输入错误']);
            }
            return json(['code' => 200, 'msg' => '转出成功']);
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }
    /**
     * 获取平台支付二维码
     */
    public function getSystemTwoCode()
    {
        return json(['code' => 200, 'msg' => Systems::getCodePhoto()]);
    }

    /**
     * 充值（微信  支付宝）
     */
    public function userRecharge()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Recharge');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            $res  = Order::addOrder($params);
            if(!$res){
                return json(['code' => 300, 'msg' => '提交失败，请稍后重试']);
            }
            return json(['code' => 200, 'msg' => '充值申请提交成功']);
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }

    /**
     * 绑定提现银行卡 支付宝
     */
    public function userBinding()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Wbidding');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            $res  = User::userBiddingForUid($params);
            if(!$res){
                return json(['code' => 300, 'msg' => '保存失败，请稍后重试']);
            }
            return json(['code' => 200, 'msg' => '保存成功']);
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }
    /**
     * 设置支付密码
     */
    public function addPaymentPassword()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Wresetpassword');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            $res  = User::addTransferPassword($params);
            if(!$res){
                return json(['code' => 300, 'msg' => '设置失败，请稍后重试']);
            }
            return json(['code' => 200, 'msg' => '设置成功']);
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }
    /**
     * 修改密码（1登录密码     2支付密码）
     */
    public function updatePassword()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Wupdatepassword');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            $res  = User::savePassword($params);
            if($res === -1){
                return json(['code' => 300, 'msg' => '旧密码输入错误']);
            }
            if(!$res){
                return json(['code' => 300, 'msg' => '修改失败，请稍后重试']);
            }
            return json(['code' => 200, 'msg' => '修改成功']);
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }

    /**
     * 证券转换美金
     */
    public function securitiesToMoney()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Securities');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            $money = floatval($params['money'])/100;
            if(floor($money) != $money || $money < 0){
                return  json(['code' => 300, 'msg' => '转出额度必须为100的倍数']);
            }
            $res  = User::toMoney($params);
            if ($res === -1) {
                return json(['code' => 300, 'msg' => '转换额度不能大于证券市值']);
            }
            if(!$res){
                return json(['code' => 300, 'msg' => '操作失败，请稍后重试']);
            }
            return json(['code' => 200, 'msg' => '操作成功']);
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }

    /**
     * 用户提交提现申请
     */
    public function userWithdrawal()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Withdrawal');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            $money = floatval($params['money']);
            if(floor($money) < 20){
                return  json(['code' => 300, 'msg' => '提现额度最少为20美金']);
            }
            $res  = User::withdrawal($params);
            if($res === -1){
                return  json(['code' => 300, 'msg' => '密码输入错误']);
            }
            if($res === -2){
                return  json(['code' => 300, 'msg' => '余额不足']);
            }
            if($res === -3){
                return  json(['code' => 300, 'msg' => '您还为绑定提现信息，请先绑定信息后在申请提现']);
            }
            if($res === -4){
                return  json(['code' => 300, 'msg' => '请先设置支付密码']);
            }
            if(!$res) {
                return json(['code' => 300, 'msg' => '提现申请失败，请稍后重试']);
            }
            return json(['code' => 200, 'msg' => '提现申请成功']);
        } else {
            return  json(['code' => 301, 'msg' => '请求方式错误']);
        }
    }

    /**
     * 转账  充值  提现记录
     */
    public function getUserRecord($page = 1)
    {
        $params = input();
        if(array_key_exists("u_id",$params) && $params['u_id'] != ''){
            $list = User::getUserRecord($params['u_id'], $params['type'], $page);
            if (!$list) {
                return json(['code' => 300, 'msg' => '暂无数据']);
            }
            return json(['code' => 200, 'msg' => $list]);
        } else {
            return json(['code' => 300, 'msg' => '未登录']);
        }
    }

    /**
     * 推广用户
     */
    public function extensionuser()
    {
        $params = input();
        if(array_key_exists("u_id",$params) && $params['u_id'] != ''){
            $list = User::offlinelist($params['u_id']);
            $money = User::downUserAllMoney($params['u_id']);
            if ($list['list'] == null) {
                return json(['code' => 300, 'msg' => '暂无数据',  'allnum' => 1, 'secnum' => 0, 'money' => $money]);
            }
            $allNum = sizeof($list['list']) + sizeof($list['lista']) + sizeof($list['listb']);
            return json(['code' => 200, 'msg' => $list, 'allnum' => $allNum, 'secnum' => sizeof($list['list']), 'money' => $money]);
        } else {
            return json(['code' => 300, 'msg' => '未登录']);
        }
    }
    /**
     * 变更手机号、邮箱
     */
    public function updatePhoneOrEmail()
    {
        $data = input();
        $validate_result = $this->validate($data, 'Wupdateuserinfo');
        if ($validate_result !== true) {
            return json(['code' => 300, 'msg' => $validate_result]);
        };
        $status = User::updateUserInfoForUid($data);
        if (!$status) {
            return json(['code' => 301, 'msg' => '变更失败']);
        }
        return json(['code' => 200, 'msg' => '变更成功']);
    }

}